/*
 * Copyright (C) 2012  Romain DUBOIS
 * 
 * This file is part of NeMoS - Network and Modular Software
 * 
 * NeMoS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * NeMoS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with NeMoS.  If not, see <http://www.gnu.org/licenses/>.
 */
package nemos.resources.impl.internal;

import java.io.File;
import java.util.Map;

import nemos.util.i18n.ITranslator;

import org.apache.felix.scr.annotations.Component;
import org.osgi.service.component.ComponentException;

/**
 * AbstractFileManager : The internal abstract file manager.
 * 
 * @author Romain DUBOIS
 */
@Component(componentAbstract = true)
class AbstractFileManager {

    /** The empty file name */
    protected static final String EMPTY_FILE_NAME = "null"; //$NON-NLS-1$

    /** The label code for an error on folder creation */
    private static final String ERROR_CREATE_DIR = "error.dir.create"; //$NON-NLS-1$
    /** The label code for an error on file type */
    private static final String ERROR_NOT_A_DIR = "error.dir.nature"; //$NON-NLS-1$
    /** The label code for an error on property type */
    private static final String ERROR_PROPERTY = "error.dir.type"; //$NON-NLS-1$

    /** The translator */
    private ITranslator mTranslator;

    /**
     * Constructor
     */
    public AbstractFileManager() {
        super();

        mTranslator = new TranslatorImpl();
    }

    /**
     * Escape a file name.
     * 
     * @param pName
     *            The file name
     * @return The escaped file name
     */
    public String escape(final String pName) {
        // Remove path separators
        String lName = pName.replace('/', '_').replace('\\', '_');

        // Then path-specific command
        while (lName.startsWith("..")) { //$NON-NLS-1$
            lName = lName.substring(2);
        }

        // Finally, ensure name is not empty
        if (lName.trim().isEmpty()) {
            lName = EMPTY_FILE_NAME;
        }

        return lName.trim();
    }

    /**
     * Check the folder designed by the specified property represents an
     * existing folder or try to create it.
     * 
     * @param pProperties
     *            The properties map
     * @param pPropertyName
     *            The property name
     * @param pDefault
     *            The default property value
     * @return The folder
     */
    public File checkOrCreateFolder(final Map<String, ?> pProperties, final String pPropertyName,
            final String pDefault) {

        // Collect default dir value
        Object lValue = pProperties.get(pPropertyName);
        if (lValue == null) {
            lValue = pDefault;
        }

        // Check value type
        if (lValue instanceof String) {

            final File lDir = new File((String) lValue);
            if (!lDir.exists()) {
                // File does not exist : try to create the folder
                if (!lDir.mkdirs()) {
                    throw new ComponentException(mTranslator.getLabel(ERROR_CREATE_DIR, lValue));
                }
            }
            // File exists but is a simple file
            else if (!lDir.isDirectory()) {
                throw new ComponentException(mTranslator.getLabel(ERROR_NOT_A_DIR, lValue));
            }
            return lDir;
        }

        // It should be a String
        throw new ComponentException(mTranslator.getLabel(ERROR_PROPERTY, pPropertyName));
    }

    /**
     * Bind the translator service.
     * 
     * @param pTranslator
     *            The translator
     */
    protected void bindTranslator(final ITranslator pTranslator) {
        mTranslator = pTranslator;
    }
}
